﻿using System.Linq;
using System.Web.Routing;

namespace System.Web.Mvc
{
    public static class AreaRegistrationContextExtensions
    {
        public static Route MapRoute(this AreaRegistrationContext context, string name, string url, bool lowercase)
        {
            return context.MapRoute(name, url, null, null, null, lowercase);
        }

        public static Route MapRoute(this AreaRegistrationContext context, string name, string url, object defaults, bool lowercase)
        {
            return context.MapRoute(name, url, defaults, null, null, lowercase);
        }

        public static Route MapRoute(this AreaRegistrationContext context, string name, string url, string[] namespaces, bool lowercase)
        {
            return context.MapRoute(name, url, null, null, namespaces, lowercase);
        }

        public static Route MapRoute(this AreaRegistrationContext context, string name, string url, object defaults, object constraints, bool lowercase)
        {
            return context.MapRoute(name, url, defaults, constraints, null, lowercase);
        }

        public static Route MapRoute(this AreaRegistrationContext context, string name, string url, object defaults, string[] namespaces, bool lowercase)
        {
            return context.MapRoute(name, url, defaults, null, namespaces, lowercase);
        }

        public static Route MapRoute(this AreaRegistrationContext context, string name, string url, object defaults, object constraints, string[] namespaces, bool lowercase)
        {
            if (lowercase)
            {
                if (namespaces == null && context.Namespaces != null)
                {
                    namespaces = context.Namespaces.ToArray();
                }
                Route route = context.Routes.MapRoute(name, url, defaults, constraints, namespaces, true);
                route.DataTokens["area"] = context.AreaName;
                route.DataTokens["UseNamespaceFallback"] = (namespaces == null || namespaces.Length == 0);
                return route;
            }
            return context.MapRoute(name, url, defaults, constraints, namespaces);
        }
    }
}